﻿/*
	Copyright (c) 2009, SpLib.net
  	All rights reserved.
  	
  	Author:6spring
  	About:网络对象
*/

package SpLib.ObjSys
{
	import SpLib.Com.Com;
	import SpLib.Com.SpLibEvent;
	import SpLib.Net.GLID;
	import SpLib.Net.GNID;
	import SpLib.Net.NetMsg;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	public class NetObjC extends SpLib.ObjSys.BaseObj
	{
		public static const JOIN_GROUP_OK:String = "SpLibObjJoinGroupOk";
		public static const JOIN_GROUP_ERR:String = "SpLibObjJoinGroupErr";
		public static const REMOTECRAETE:String = "SpLibObjRemoteCreate";
		public static const BREAK_PLAYER:String = "SpLibObjBreakPlayer";
		public static const SERVICE_BREAK:String = "SpLibObjServiceBreak";
		
		public static const ACT_TIME_DELAY:int = 30*1000;
		
		public static const ST_NONE:int = 0;
		public static const ST_JOINING:int = 1;
		public static const ST_ACT:int = 2;
		public static const ST_IDEL:int = 3;
		
		public var m_GLID:SpLib.Net.GLID = new SpLib.Net.GLID;
		public var m_GNID:SpLib.Net.GNID = new SpLib.Net.GNID;
		public var m_bIsMainPlayer:Boolean = false;
		private var m_ActTimer:Timer = null;
		private var m_nState:int = ST_NONE;
		
		public var m_nPing:int = 65535;
		
		public function NetObjC()
		{
			super();
			
			RegEvent("SendPingRet",SendPingRet);
			RegEvent("RemoteCreateMe",RemoteCreateMe);
			
			RegEvent("AOI_InArea",AOI_InArea);
			RegEvent("AOI_OutArea",AOI_OutArea);
		}
		
		public function JoinGroup(sname:String,gname:String,classname:String,agv:Array,glid:SpLib.Net.GLID=null):void
		{
			m_nState = ST_JOINING;
			
			var msg:SpLib.Net.NetMsg = new SpLib.Net.NetMsg();
			msg.m_nMsgtype = SpLib.Net.NetMsg.TYPE_SESSION_JOIN;
			msg.m_sSName = sname;
			msg.m_sGName = gname;
			
			msg.m_Value = new Array;
			msg.m_Value.push(sname);
			msg.m_Value.push(gname);
			msg.m_Value.push(classname);
			msg.m_Value.push(agv);
			
			if(glid!=null)
			{
				msg.m_GLID = glid;
				m_GLID = glid;
			}
			
			m_GNID.m_nServiceID = SpLib.Com.Str2Key(sname);
			m_GNID.m_nGroupID = SpLib.Com.Str2Key(gname);
			m_GNID.m_nObjIDH = 0;
			m_GNID.m_nObjIDL = getTimer();
			msg.m_Address.m_nObjIDL = getTimer();
			
			m_bIsMainPlayer = true;
			
			SpLib.ObjSys.NetObjMgr.GetInstance().AddJoin(m_GNID,this);
			
			SpLib.Net.NetMsgMgr.SendMsg(msg);
		}
		
		//仅仅主玩家允许ToS
		public function ToS(evename:String,agv:Array = null):void
		{
			if(m_bIsMainPlayer) return;
			
			var msg:SpLib.Net.NetMsg = new SpLib.Net.NetMsg();
			msg.m_nMsgtype = SpLib.Net.NetMsg.TYPE_SESION_SENDS;
			msg.m_GLID = m_GLID;
			msg.m_Address = m_GNID;
			msg.m_sEveName = evename;
			msg.m_Value = agv;
			
			SpLib.Net.NetMsgMgr.SendMsg(msg);
		}
		
		private function SendPing(e:flash.events.Event):void
		{
			var agv:Array = new Array;
			agv.push(getTimer());
			ToS("Ping",agv);
		}
		
		private function SendPingRet(e:SpLibEvent):void
		{
			m_nPing = getTimer() - e.m_Agv[0] as int;
			m_nPing = m_nPing/2;
			
			var agv:Array = new Array;
			agv.push(m_nPing);
			ToS("SetPing",agv);
		}
		
		public function JoinGruopOk(glid:GLID,gnid:GNID):void
		{			
			m_GLID = glid;
			m_GNID = gnid;
			
			m_ActTimer = new Timer(ACT_TIME_DELAY);
			m_ActTimer.addEventListener(TimerEvent.TIMER,SendPing);
			m_ActTimer.start();
			
			m_nState = ST_ACT;
			this.dispatchEvent(new SpLibEvent(JOIN_GROUP_OK,"OnJoinGruopOk"));
			
			this.addEventListener(SpLibEvent.OBJDELTHIS,DelNetObj);
		}
		
		private function DelNetObj(evt:SpLibEvent):void
		{
			PlayerBreak();
		}
		
		public function RemoteCreateMe(e:SpLibEvent):void
		{
			this.dispatchEvent(new SpLibEvent(REMOTECRAETE,"OnRemoteCreate",e.m_Agv));
		}
		
		public function JoinGroupErr(errflag:int,errmsg:String):void
		{
			this.dispatchEvent(new SpLibEvent(JOIN_GROUP_ERR, "OnJoinGroupErr", [errflag, errmsg]));
		}
		
		public function RemouteCreate(glid:GLID,gnid:GNID,agv:Array):void
		{
			m_GLID = glid;
			m_GNID = gnid;
			
			
			this.dispatchEvent(new SpLibEvent(REMOTECRAETE,"OnRemoteCreate",agv));
		}
		
		public function BreakPlayer(flag:int,msg:String):void
		{
			this.dispatchEvent(new SpLibEvent(BREAK_PLAYER,"OnBreakPlayer",[flag,msg]));
			m_nState = ST_IDEL;
		}
		
		public function ServiceBreak(serviceid:uint):void
		{
			this.dispatchEvent(new SpLibEvent(SERVICE_BREAK,"OnServiceBreak",[serviceid]));
			m_nState = ST_IDEL;
		}
		
		//玩家自己主动离开
		public function PlayerBreak():void
		{			
			if(m_nState==ST_ACT && m_bIsMainPlayer)
			{		
				m_nState = ST_IDEL;
						
				var msg:SpLib.Net.NetMsg = new SpLib.Net.NetMsg();
				msg.m_nMsgtype = SpLib.Net.NetMsg.TYPE_SESSION_LEAVE;
				msg.m_GLID = m_GLID;
				msg.m_Address = m_GNID;	
				SpLib.Net.NetMsgMgr.SendMsg(msg);

				SpLib.ObjSys.NetObjMgr.DelGroup(m_GNID.m_nServiceID,m_GNID.m_nGroupID);
			}
		}
		
		private function AOI_InArea(e:SpLibEvent):void
		{
			
		}
		
		private function AOI_OutArea(e:SpLibEvent):void
		{
			
		}
	}
}